﻿using Application_Core.Dispatcher_Component.Access_Layer;
using RabbitMQ_Service.MessageTypes;
using System;
using System.Collections.Generic;
using Utility.Global.DataTypes;
using Utility.RabbitMQ_Service;
using Utility.RabbitMQ_Service.Implementations;

namespace Application_Core.Monitor_Component.Business_Logic_Layer
{
    class MonitorComponentBusinessLogicImpl : MonitorComponentBusinessLogic
    {
        private readonly DispatcherForMonitorService dispatcherForMonitorService;
        private IDictionary<String, SystemTyp> systeme;

        public MonitorComponentBusinessLogicImpl(DispatcherForMonitorService dispatcherForMonitorService)
        {
            this.dispatcherForMonitorService = dispatcherForMonitorService;
            this.systeme = dispatcherForMonitorService.FindSystemStatus();
        }

        public List<SystemTyp> FindSystemStatus()
        {
            List<SystemTyp> systeme = new List<SystemTyp>();
            foreach (SystemTyp system in this.systeme.Values)
            {
                systeme.Add(system);
            }
            return systeme;
        }

        public void StartIAmAliveHandler()
        {
            using (RabbitMQService rabbitMQService = RabbitMQServiceImpl.CreateRabbitMQService()) {
                using (RabbitMQQueue<IAmAliveMessage> queue = rabbitMQService.NewRabbitMQQueue<IAmAliveMessage>("monitor.iamalive"))
                {
                    while (true)
                    { 
                        IAmAliveMessage iamalive = queue.Receive();
                        if (iamalive.Ok == true)
                        {
                            this.NewIAmAlive(iamalive.SystemName);
                        }
                    }
                }
            }
        }

        public bool StartSystem(string systemName)
        {
            this.SetSystemStatus(systemName, SystemState.Status.Online);
            return dispatcherForMonitorService.StartSystem(systemName);
        }

        public bool StopSystem(string systemName)
        {
            SystemTyp system;
            this.systeme.TryGetValue(systemName, out system);
            system.AnzahlDisconnected++;
            system.Status = SystemState.Status.Offline;
            system.LetztesUpdate = DateTime.Now;
            return dispatcherForMonitorService.StopSystem(systemName);
        }

        private void SetSystemStatus(String systemName, SystemState.Status status) {
            SystemTyp system;
            this.systeme.TryGetValue(systemName, out system);
            system.Status = status;
            system.LetztesUpdate = DateTime.Now;
            this.systeme[systemName] = system;
        }

        private void NewIAmAlive(String systemName)
        {
            SystemTyp system;
            this.systeme.TryGetValue(systemName, out system);
            system.LetztesIAmAlive = DateTime.Now;
            this.systeme[systemName] = system;
        }
    }
}
